//
//  NSObject+shareTestThread.m
//  Gunterapple
//
//  Created by 热带雨林 on 2021/1/21.
//  Copyright © 2021 热带雨林. All rights reserved.
//

#import "NSObject+shareTestThread.h"

@implementation NSObject (shareTestThread)
#pragma mark - 后台常驻线程

//单例
+ (instancetype)shareThread{
    static NSThread *shareThread = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareThread = [[NSThread alloc]initWithTarget:self selector:@selector(threadTest) object:nil];
        //设置名称
        [shareThread setName:@"threadTest"];
        //线程开启
        [shareThread start];
    });
    return shareThread;
}

+ (void)threadTest{
    @autoreleasepool {
        NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    //给当前的runLoop添加端口源，防止mach消息发送到线程之后，指定线程的runloop没有开启，内核需要保留这次消息，等到下一次runloop循环中
        [runLoop addPort:[NSMachPort port] forMode:NSRunLoopCommonModes];
        [runLoop run];
    }
}

@end
